package com.ksu.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.ksu.manager.model.User;
import com.ksu.manager.service.IUserService;

/**
 * 登录controller
 * 
 * @author lambert
 * 
 */

@Controller
@RequestMapping("/login")
public class LoginController {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(LoginController.class);
	private IUserService userService;



	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value = "/logon")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response,User user) {
		ModelAndView mv = new ModelAndView("/login/result");
		try {
			List<User> userList = userService.queryList(user);
			if(userList!=null && userList.size()>0){
				mv.addObject("result", "success");
				HttpSession session = request.getSession();
				session.setAttribute("user", userList.get(0));
			}else{
				mv.addObject("result", "faild");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mv;
	}
	
	@RequestMapping(value = "/logonOut")
	public ModelAndView loginOut(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView("/login/result");
		try{
			HttpSession session = request.getSession();
			if(session.getAttribute("user")!=null){
				session.removeAttribute("user");
			}
			mv.addObject("result", "loginOutSuccess");
		}catch(Exception e){
			e.printStackTrace();
		}
		return mv;
	}

}
